<?php 
class JobInfoController extends AppController{
	var $name = 'JobInfo';
	var $uses = array('SysParams','Job','Company','MailJob','CompanyAccount','JobHopeMember','JobHopeLanguage','JobCategory','JobAttention','JobArea','JobPost','JobPickupArea','JobPickupThema','JobAttentionArea','JobAttentionThema');
	var $access = array (
							'index' => array('role' => array('System')),
							'search' => array('role' => array('System','Admin','Staff')),
							'showList' => array('role' => array('System','Admin','Staff')),
							'delete' => array('role' => array('System','Admin','Staff')),
							'excelDownload' => array('role' => array('System','Admin','Staff')), 
							'detail' => array('role' => array('System','Admin','Staff')),  
							'job_republic' => array('role' => array('System','Admin','Staff')),
							'job_stop' => array('role' => array('System','Admin','Staff')),
							'job_approve' => array('role' => array('System')),
							'approve_finish' => array('role' => array('System')),
							'job_unapprove' => array('role' => array('System')),
							'edit_init' => array('role' => array('System','Admin','Staff')),
							'job_edit' => array('role' => array('System','Admin','Staff')),
							'job_edit_confirm' => array('role' => array('System','Admin','Staff')),
							'job_edit_finish' => array('role' => array('System','Admin','Staff')),
							'job_add' => array('role' => array('System','Admin','Staff')),
							'confirm_init' => array('role' => array('System','Admin','Staff')),
							'job_add_finish' => array('role' => array('System','Admin','Staff')),
							'address_list' => array('role' => array('System','Admin','Staff')),
							'tema_list' => array('role' => array('System','Admin','Staff'))
						);
	function index(){
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報検索
	 */
	function search($arg1=null,$id=null){
		
		if(!empty($arg1)&&!empty($id)){
			if($arg1=='area_today'){
				$this->data['JobInfo']['job_area_id']=$id;
				$this->data['JobInfo']['if_today']="(1=1) and ";
			}
			if($arg1=='tema_today'){
				$this->data['JobInfo']['category_id']=$id;
				$this->data['JobInfo']['if_today']="(1=1) and ";
			}
			if($arg1=='area_all'){
				$this->data['JobInfo']['job_area_id']=$id;
//				$this->data['JobInfo']['public_start']=date('Y-m-d');
				$this->data['JobInfo']['public_end']=date('Y-m-d');
				$this->data['JobInfo']['job_end']=date('Y-m-d');
			}
			if($arg1=='tema_all'){
				$this->data['JobInfo']['category_id']=$id;
//				$this->data['JobInfo']['public_start']=date('Y-m-d');
				$this->data['JobInfo']['public_end']=date('Y-m-d');
				$this->data['JobInfo']['job_end']=date('Y-m-d');
			}
			$this->data['JobInfo']['job_status_id']="1";
			$this->data['JobInfo']['public_close']="(1=1) and ";
		}

		if(!empty($this->data)){
			$this->Session->write('job_info_search_data',$this->data);
			$this->redirect('/JobInfo/showList');			
		}
		
		$post_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'post_id' and \"SysParams\".\"deleted\" is null");
		$lang_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'hope_language_id' and \"SysParams\".\"deleted\" is null",'sort');
		$memb_type = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'hope_member_id' and \"SysParams\".\"deleted\" is null");
		$attention  = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'attention_id' and \"SysParams\".\"deleted\" is null");
		$area_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'area_id' and \"SysParams\".\"deleted\" is null");
		$tema_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'tema_id' and \"SysParams\".\"deleted\" is null");
		
		$this->set('post_data',$post_data);
		$this->set('lang_data',$lang_data);
		$this->set('memb_data',$memb_type);
		$this->set('attention',$attention);
		$this->set('area_data',$area_data);
		$this->set('tema_data',$tema_data);
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報一覧
	 */
	function showList($status='',$flg=null){
		$condition = '';
		if(empty($status)){
			$search_data = $this->Session->read('job_info_search_data');
			if(empty($search_data)){
				$this->redirect('/JobInfo/search');
			}
			$condition = $this->Job->createCondition($this->Search,$search_data,$this->rdAuth->company_id);
		    $this->Session->write('search_condition',$condition);
		}
		else{
			$condition = $this->Job->getStatusCondition($this->Search,$status,$this->rdAuth->company_id);
		    if(!empty($flg)){
		    	$search_condition=$this->Session->read('search_condition');
		    	if(!empty($search_condition)){
		    		$condition.=' and '.$this->Session->read('search_condition');
		    	}
		    }else{
		    	$this->Session->del('search_condition');
		    }
		}
		$this->Session->write('job_search_condition',$condition);
		$order = 'Job.created desc';
		$count = $this->Job->findCount($condition);
		$this->setPaging('ajax','/JobInfo/showList/'.$status,$count);
		
		$this->Job->hasMany = array();
		$data = $this->Job->findAll($condition,null,$order,$this->show,$this->page);
		$this->setValue($data);
		$front_url = FRONT_SERVER_NAME;
		$this->set('front_url',$front_url);
		$this->set('data',$data);
		$this->set('count',$count);
		$this->set('page',$this->page);
		$this->set('status',$status);
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報削除
	 */
	function delete($job_id=null){
		if(empty($job_id)){
			$id = $_COOKIE['job_list_delete'];
			if(!empty($id)){
				$this->Job->deleteJob($id);
				$this->delCookie('job_list_delete');
			}
		}
		else{
			$this->Job->deleteJob($job_id);
		}
		$this->redirect('/JobInfo/showList');
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報Excelダウンロード
	 */
	function excelDownload($status=''){
		
		//$id = $_COOKIE['job_list_delete'];
		//$this->delCookie('job_list_delete');
		$condition = $this->Session->read('job_search_condition');
		$order = 'Job.created desc';
		$data = $this->Job->findAll($condition,null,$order,null,null,2);
		$data = $this->Job->processExcelData($data);
		$file_name = "job_data_" . date("YmdHis");
		$col_name = $this->Job->getExcelCol();
		$this->Excel->createExcel($file_name,$col_name,$data);
		exit;
	}
	
	/**
	 * @desc 求人情報詳細
	 * @author yangjie
	 */
	function detail($job_id,$page=1,$status=''){
		$condition = "\"Job\".deleted is null and \"Job\".id=".$job_id;
		$data = $this->Job->find($condition,null,null,2);
		$this->checkAccess1($data['Job']['company_id'],$this->rdAuth->company_id);
		$this->data = $data;
		if(!empty($data['JobPickupArea'])){ 	
		 	$this->data['Job']['address1'] = trim($this->processInner2($data,'JobPickupArea','area_id'));
		 	$this->data['Job']['job_pickup_addr']=implode(',',$this->processInner1($data,'JobPickupArea','area_id'));
		}
		if(!empty($data['JobPickupThema'])){
		 	$this->data['Job']['tema1'] = trim($this->processInner2($data,'JobPickupThema','tema_id'));
		 	$this->data['Job']['job_pickup_tema']=implode(',',$this->processInner1($data,'JobPickupThema','tema_id'));
		}
	 	if(!empty($data['JobAttentionArea'])){
		 	$this->data['Job']['address2'] = trim($this->processInner2($data,'JobAttentionArea','area_id'));
		 	$this->data['Job']['job_attention_addr']=implode(',',$this->processInner1($data,'JobAttentionArea','area_id'));
	 	}
	 	if(!empty($data['JobAttentionThema'])){
		 	$this->data['Job']['tema2'] = trim($this->processInner2($data,'JobAttentionThema','tema_id'));
		 	$this->data['Job']['job_attention_tema']=implode(',',$this->processInner1($data,'JobAttentionThema','tema_id'));
	 	}
	 	if(!empty($data['Job']['attention_img_file'])){
	 		$this->data['Job']['job_img_path']='/'.$this->Prop->getValue('config.job.image.dir').'/'.floor($this->data['Job']['id']/1000).'/'.$this->data['Job']['id'].'/'.$this->data['Job']['attention_img_file'];
	 	}
		$this->data['Job']['page']=$page;
		$this->data['Job']['status']=$status;
		$this->Session->write('job_status',$status);
		$this->Session->write('detail_url','/'.$this->params['controller'].'/'.$this->params['action']);
		$this->Session->write('job_status_page',$page);
        $this->render('detail');
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報再開
	 */
    function job_republic($status=''){
		$this->Job->republic($this->data['Job']['id']);
		$this->redirect('/JobInfo/detail/'.$this->data['Job']['id'].'/'.$this->data['Job']['page'].'/'.$status);
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報停止
	 */
	function job_stop($status=''){
		$this->data['Job']['public_close_date'] = date('Y-m-d H:i:s');
		$this->Job->save($this->data);
		$this->redirect('/JobInfo/detail/'.$this->data['Job']['id'].'/'.$this->data['Job']['page'].'/'.$status);
	}
	
	/**
	 * @auth yangjie
	 * @desc 求人情報承认
	 */
	function job_approve(){
		$condition = "\"Job\".\"deleted\" is null and \"Job\".\"id\"=".$this->data['Job']['id'];
		$page = $this->data['Job']['page'];
		$status = $this->data['Job']['status'];
		
		//承认状态选择验证
		$message = $this->Valid->ErrorMsg($this->data,'Job/job_status_id','isChoose',true);
		if($message){
			$this->set('message',$message);
			$this->detail($this->data['Job']['id'],$page,$status);
			exit;
		}
		
		$data = $this->Job->find($condition,null,null,2);	

		if($this->data['Job']['job_status_id']!=1){
			$this->data = $data;
			$this->data['Job']['page']=$page;
			$this->data['Job']['status']=$status;
			$this->render('job_unapprove');
			exit;
		}	
		$this->set('public_hour',$this->getHourSelect());
		$this->set('public_min',$this->getMinSelect());
		$area_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'area_id' and \"SysParams\".\"deleted\" is null");
		$tema_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'tema_id' and \"SysParams\".\"deleted\" is null");		
		$this->set('area_data',$area_data);
		$this->set('tema_data',$tema_data);
		
		$this->data = $data;
		$this->set('job',$data);
		if(empty($data['Job']['public_end'])){
			$this->data['Job']['public_end']=date("Y-m-d", mktime(0, 0, 0, date("m")+2  , date("d"), date("Y")));;
		}
		if(empty($data['Job']['company_name_flg'])&&$data['Job']['company_name_flg']!=0){
			$this->data['Job']['company_name_flg']=2;
			$this->data['Job']['company_name_other']='仲介募集';
		}
		$this->data['Job']['job_area']=$this->processInner1($data,'JobArea','job_area_id');
		$this->data['Job']['category']=$this->processInner1($data,'JobCategory','category_id');	
		if(!empty($data['JobPickupArea'])){ 	
		 	$this->data['Job']['address1'] = trim($this->processInner2($data,'JobPickupArea','area_id'));
		 	$this->data['Job']['job_pickup_addr']=implode(',',$this->processInner1($data,'JobPickupArea','area_id'));
		}
		if(!empty($data['JobPickupThema'])){
		 	$this->data['Job']['tema1'] = trim($this->processInner2($data,'JobPickupThema','tema_id'));
		 	$this->data['Job']['job_pickup_tema']=implode(',',$this->processInner1($data,'JobPickupThema','tema_id'));
		}
	 	if(!empty($data['JobAttentionArea'])){
	 	$this->data['Job']['address2'] = trim($this->processInner2($data,'JobAttentionArea','area_id'));
	 	$this->data['Job']['job_attention_addr']=implode(',',$this->processInner1($data,'JobAttentionArea','area_id'));
	 	}
	 	if(!empty($data['JobAttentionThema'])){
	 	$this->data['Job']['tema2'] = trim($this->processInner2($data,'JobAttentionThema','tema_id'));
	 	$this->data['Job']['job_attention_tema']=implode(',',$this->processInner1($data,'JobAttentionThema','tema_id'));
	 	}
	 	if(!empty($this->data['Job']['attention_img_file'])){
			$this->data['Job']['job_img_path']='/'.$this->Prop->getValue('config.job.image.dir').'/'.floor($this->data['Job']['id']/1000).'/'.$this->data['Job']['id'].'/'.$this->data['Job']['attention_img_file'];
		}else{
			$this->data['Job']['job_img_path']=null;
		}
		$this->data['Job']['page']=$page;
		$this->data['Job']['status']=$status;
	}
	
	function processInner1($data,$inner,$field){
	    if(empty($data[$inner])){
	    	return array();
	    }	
	    $arr=array();
	    foreach($data[$inner] as $inner){
			$arr[]=$inner[$field];
		}
		return $arr;
	}
	
	function processInner2($data,$key,$kbn){
		if(empty($data[$key])){
			return '';
		}
		$arr = array();
		foreach($data[$key] as $d){
			$sysdata=null;
			if($key=='JobArea'){
				$sysdata=$this->SysParams->find("\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"id\"=".$d[$kbn]);
			}elseif($key=='JobCategory'){
				$sysdata=$this->SysParams->find("\"SysParams\".\"kbn\"='tema_id' and \"SysParams\".\"id\"=".$d[$kbn]);
			}else{
			    $sysdata=$this->SysParams->find('"SysParams"."kbn"='.'\''.$kbn.'\''.' and "SysParams"."id"='.$d[$kbn]);
			}
			if($key=='JobHopeLanguage'&&$d[$kbn]==4){
				$arr[] = trim($data['Job']['hope_language_note']);
			}else{
				$arr[] = trim($sysdata['SysParams']['name']);
			}
		}		
		return  implode(',',$arr);	
	}
	
	function processInner3($data,$field,$kbn){
		if(empty($data[$field])){
			return '';
		}
		$arr = array();
		foreach($data[$field] as $d){
			$sysdata=$this->SysParams->find('"SysParams"."kbn"='.'\''.$kbn.'\''.' and "SysParams"."id"='.$d);
			$arr[] = $sysdata['SysParams']['name'];
		}
		return  implode(',',$arr);
	}
	
	function approve_confirm(){
		$job=$this->data;
		$form=$this->params['form'];		
		if(!empty($job['Job']['job_pickup_addr'])){
	    $job['Job']['job_pickup_addr']=explode(',',$job['Job']['job_pickup_addr']);
	    }
	    if(!empty($job['Job']['job_pickup_tema'])){
	    $job['Job']['job_pickup_tema']=explode(',',$job['Job']['job_pickup_tema']);
	    }
	    if(!empty($job['Job']['job_attention_addr'])){
	    $job['Job']['job_attention_addr']=explode(',',$job['Job']['job_attention_addr']);
	    }
	    if(!empty($job['Job']['job_attention_tema'])){
	    $job['Job']['job_attention_tema']=explode(',',$job['Job']['job_attention_tema']);
	    }	
		$message=$this->Job->valid_approve($this->Valid,$job);
	 		
 		if($job['Job']['job_img_del']==0&&$form['job_img_file']['name']['0']){			
	    $max_file_size=$this->Prop->getValue('config.upload_size');
		$img_message = $this->Valid->ErrorMsg($this->params['form'], 'job_img_file/error', 'upload_error');
		$img_message .= $this->Valid->ErrorMsg($this->params['form'], 'job_img_file/type', 'image_type');
		$img_message .= $this->Valid->ErrorMsg($this->params['form'], 'job_img_file/size', 'file_size', $max_file_size);
		$message.=$img_message;
	    }		
	    if($message){
		$this->set('message', $message);
		$condition = "\"Job\".\"deleted\" is null and \"Job\".\"id\"=".$this->data['Job']['id'];
		$data = $this->Job->find($condition,null,null,2);	
		$this->set('public_hour',$this->getHourSelect());
		$this->set('public_min',$this->getMinSelect());
		$area_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'area_id' and \"SysParams\".\"deleted\" is null");
		$tema_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'tema_id' and \"SysParams\".\"deleted\" is null");
		$this->set('area_data',$area_data);
		$this->set('tema_data',$tema_data);	
		$this->set('job',$data);
		$this->render('job_approve');
		exit;
	    }
		if(!empty($form['job_img_file']['name']['0']) && $job['Job']['job_img_del']==0){
		    $this->Image->dir_confirm=array($this->Prop->getValue('config.job.image.dir_confirm'));
			$this->Image->rdImage->resizeWidth = array('128');
			$this->Image->rdImage->resizeHeight = array('100');
			if ($this->Image->saveConfirmPhoto($form, 'job_img_file')) {
				$dir_confirm = $this->Image->dir_confirm;
				$filename = $this->Image->filename;
				$job['Job']['job_img'] = $filename[0];
		    }
		}
		if($job['Job']['job_img_del']==0){ 
        	if(!empty($form['job_img_file']['name']['0'])){   
            $job['Job']['job_img_path']='/'.$this->Prop->getValue('config.job.image.dir_confirm').'/'.$job['Job']['job_img'];
            }
		}else{
			$job['Job']['job_img_path']=null;
		}
		$job['Job']['job_areas']=$this->processInner3($job['Job'],'job_area','area_id');
		$job['Job']['job_temas']=$this->processInner3($job['Job'],'category','tema_id');
		if(!empty($job['Job']['important_area_id'])){
		$sys=$this->SysParams->find("\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"deleted\" is null and \"SysParams\".\"id\"=".$job['Job']['important_area_id']);
        $job['Job']['important_area']=$sys['SysParams']['name'];
		}
		$this->set('job',$job);
		$company=$this->Company->find('"Company"."id"='.$job['Job']['company_id']);
		$this->set('company',$company);
		//pr($company);
		$condition = "\"Job\".\"deleted\" is null and \"Job\".\"id\"=".$this->data['Job']['id'];
		$data = $this->Job->find($condition,null,null,2);
		$this->data = $data;
		$job['Job']['approve_date']=date('Y-m-d H:i:s',time());
		//pr($job);
		$this->Session->write('job_approve',$job);
	}
	
	function approve_save(){
		$job_info=$this->Session->read('job_approve');
		$job_info['Job']['job_status_id']=1;
		if(empty($job_info['Job']['public_start'])){
			$job_info['Job']['public_start']=null;
		}
		if($job_info['Job']['company_name_flg']!=2){
			$job_info['Job']['company_name_other']=null;
		}
		if(empty($job_info['Job']['public_end'])){
			$job_info['Job']['public_end']=null;
		}
		if(empty($job_info['Job']['pickup_start'])){
			$job_info['Job']['pickup_start']=null;
		}
		if(empty($job_info['Job']['pickup_end'])){
			$job_info['Job']['pickup_end']=null;
		}
		if(empty($job_info['Job']['attention_start'])){
			$job_info['Job']['attention_start']=null;
		}
		if(empty($job_info['Job']['attention_end'])){
			$job_info['Job']['attention_end']=null;
		}
		if(empty($job_info['Job']['important_area_id'])){
			$job_info['Job']['important_area_id']=null;
		}
		if(empty($job_info['Job']['important_tema_id'])){
			$job_info['Job']['important_tema_id']=null;
		}
		if($job_info['Job']['job_img_del']==0 && !empty($job_info['Job']['job_img'])){
			$job_info['Job']['attention_img_file']=$job_info['Job']['job_img'];
		}
		
		if($this->Job->save($job_info)){
			$job_id=$job_info['Job']['id'];	
			
			if($job_info['Job']['job_img_del']==1){
		    	$this->Job->delImgFile($job_id);
		    }		    
			if($job_info['Job']['job_img_del']==0 && !empty($job_info['Job']['job_img'])){
			$this->Image->dir_confirm=array($this->Prop->getValue('config.job.image.dir_confirm'));
			$path = floor($job_id/1000). DS .$job_id;
			$this->Image->dir=array($this->Prop->getValue('config.job.image.dir'));
			$this->Image->savePhoto($path,$job_info['Job']['job_img']);		
			}
			
			if(!empty($job_info['Job']['job_pickup_addr'])){    	
			    $this->JobPickupArea->delByJobid($job_id);
				foreach($job_info['Job']['job_pickup_addr'] as $pickup_area){
				$this->JobPickupArea->save(array("job_id"=>$job_id,"area_id"=>$pickup_area));
				}
			}else{
				$this->JobPickupArea->delByJobid($job_id);
			}
		    if(!empty($job_info['Job']['job_pickup_tema'])){		    	
			    $this->JobPickupThema->delByJobid($job_id);
				foreach($job_info['Job']['job_pickup_tema'] as $pickup_tema){
				$this->JobPickupThema->save(array("job_id"=>$job_id,"tema_id"=>$pickup_tema));
				}
			}else{
				$this->JobPickupThema->delByJobid($job_id);
			}
			if(!empty($job_info['Job']['job_attention_addr'])){    	
			    $this->JobAttentionArea->delByJobid($job_id);
				foreach($job_info['Job']['job_attention_addr'] as $attention_area){
				$this->JobAttentionArea->save(array("job_id"=>$job_id,"area_id"=>$attention_area));
				}
			}else{
				$this->JobAttentionArea->delByJobid($job_id);
			}
			if(!empty($job_info['Job']['job_attention_tema'])){    	
			    $this->JobAttentionThema->delByJobid($job_id);
				foreach($job_info['Job']['job_attention_tema'] as $attention_tema){
				$this->JobAttentionThema->save(array("job_id"=>$job_id,"tema_id"=>$attention_tema));
				}
			}else{
				$this->JobAttentionThema->delByJobid($job_id);
			}
			if(!empty($job_info['Job']['job_area'])){
				$this->JobArea->delByJobid($job_id);
				foreach($job_info['Job']['job_area'] as $job_area){
				$this->JobArea->save(array("job_id"=>$job_id,"job_area_id"=>$job_area));
				}
			}
			if(!empty($job_info['Job']['category'])){
				$this->JobCategory->delByJobid($job_id);
				foreach($job_info['Job']['category'] as $category){
				$this->JobCategory->save(array("job_id"=>$job_id,"category_id"=>$category));
				}
			}			
			$company=$this->Company->find('"Company"."id"='.$job_info['Job']['company_id']);
			$subject=$this->Prop->getValue('config.mail_title.job_approve');
			$subject=str_replace('#id#',$job_id, $subject);		
            $job=$this->Job->find('"Job"."id"='.$job_id);
            $subject=str_replace('#month#', date('m',strtotime($job['Job']['public_start'])), $subject);
			$subject=str_replace('#day#', date('d',strtotime($job['Job']['public_start'])), $subject);
			$account=$this->CompanyAccount->find('"CompanyAccount"."id"='.$job_info['Job']['admin_id']);
			$data['name']=$account['CompanyAccount']['name'];
			$data['job_id']=$job_id;
			$data['title']=$job_info['Job']['title'];
			$data['created']=$job['Job']['created'];
			$data['url_admin']="top/index/job_info/detail/$job_id";
			$data['url_front']="docs/$job_id";
			$data['public_start']=$job_info['Job']['public_start'];
			$data['public_start_hour']=$job_info['Job']['public_start_hour'];
			$data['public_start_min']=$job_info['Job']['public_start_min'];
			$data['public_end']=$job_info['Job']['public_end'];
			$data['public_end_hour']=$job_info['Job']['public_end_hour'];
			$data['public_end_min']=$job_info['Job']['public_end_min'];
			$data['company_type_id']=$company['Company']['company_type_id'];
			$data['front_server_name']=FRONT_SERVER_NAME;
			$this->autoRender = false;
			$contents=$this->getMailTemplate("job_approve",$data);

			//求人投稿担当者メールアドレス
			$this->MailJob->saveMsg('web@localhost:82',$account['CompanyAccount']['email'],$subject,$contents,'','info@localhost:82');
			
		}
		$this->Session->del('Job_Id');
		$this->redirect('job_info/job_edit_finish');
	}
	
	function job_unapprove(){
		if(!empty($this->data['Job']['refuse_reason'])){
			$message = $this->Valid->ErrorMsg($this->data,'Job/refuse_reason','mbmaxlength',300);
		}
		$condition = "\"Job\".\"deleted\" is null and \"Job\".\"id\"=".$this->data['Job']['id'];
		$data = $this->Job->find($condition,null,null,2);
		$page = $this->data['Job']['page'];
		$status = $this->data['Job']['status'];
		$refuse_reason = $this->data['Job']['refuse_reason'];
		$this->data = $data;
		$this->data['Job']['refuse_reason']=$refuse_reason;
		$this->data['Job']['page']=$page;
		$this->data['Job']['status']=$status;
		
		if($message){
			$this->set('message',$message);
			$this->render('job_unapprove');
			exit;
		}

		$this->Job->save($this->data);
		
		$subject = $this->Prop->getValue('config.mail_title.job_unapprove');
		$subject=str_replace('#id#',$this->data['Job']['id'], $subject);
		$contents=$this->getMailTemplate("job_unapprove",$this->data);
		
		$account=$this->CompanyAccount->find('"CompanyAccount"."id"='.$data['Job']['admin_id']);
		//求人投稿担当者メールアドレス
		$this->MailJob->saveMsg('web@localhost:82',$account['CompanyAccount']['email'],$subject,$contents,'','info@localhost:82');
		
		if($this->data['Job']['job_status_id']!=2){
			$this->data['Job']['job_status_id']=2;
			$this->Job->save($this->data);
			/*if($this->data['Company']['job_approved']>=0){
				$approve=$this->data['Company']['job_approved']+1;
			    $this->Company->approveDesc($this->data['Company']['id'],$approve);
			}*/
		}
		$this->redirect('/JobInfo/showList/'.$this->data['Job']['status'].'?page='.$this->data['Job']['page']);
	}
	/**
	 * @author lizeqiu
	 * @desc job编辑初始化
	 * */
	function edit_init($job_id){
		$condition = "\"Job\".\"deleted\" is null and \"Job\".\"id\"=".$job_id;
		$job_init = $this->Job->find($condition,null,null,2);
		$company=$this->Company->find('"Company"."id"='.$job_init['Job']['company_id']);
		
		$post_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'post_id' and \"SysParams\".\"deleted\"is null");
		$lang_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'hope_language_id' and \"SysParams\".\"deleted\" is null",'sort');
		$memb_type = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'hope_member_id' and \"SysParams\".\"deleted\" is null");
		$area_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'area_id' and \"SysParams\".\"deleted\" is null");
		$tema_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'tema_id' and \"SysParams\".\"deleted\" is null");	    
		$attention_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'attention_id' and \"SysParams\".\"deleted\" is null");

// 		if($this->rdAuth->role=='Admin'&&$job_init['Job']['admin_id']!=1){
			$this->CompanyAccount->displayField = 'name';
			$all_accs=$this->CompanyAccount->generateList("\"CompanyAccount\".\"company_id\"=".$company['Company']['id']." and \"CompanyAccount\".\"deleted\" is null");		
			$this->set('all_accs',$all_accs);
// 		}
		$this->set('areas',$area_data);
		$this->set('tema_data',$tema_data);
		$this->set('post_data',$post_data);
		$this->set('lang_data',$lang_data);
		$this->set('member_type',$memb_type);		
		$this->set('attention_data',$attention_data);		
		$this->set('company',$company);
		$this->set('detail_url',$this->Session->read('detail_url'));
	}
	/**
	 * @author lizeqiu
	 * @desc job编辑页面
	 * */
	function job_edit($job_id,$flg=null){
		$ajob=$this->Job->find('Job.id='.$job_id);
		$this->rdAuth->company_id=1;
		$this->checkAccess1($ajob['Job']['company_id'],$this->rdAuth->company_id);
		$this->set('public_hour',$this->getHourSelect());
		$this->set('public_min',$this->getMinSelect());
		$this->edit_init($job_id);
		if(empty($this->data)){
			if(empty($flg)){
				$condition = "\"Job\".\"deleted\" is null and \"Job\".\"id\"=".$job_id;
			    $this->data=$this->Job->find($condition,null,null,2);

				if(!empty($this->data['Job']['attention_img_file'])){
					$this->data['Job']['job_img_path']='/'.$this->Prop->getValue('config.job.image.dir').'/'.floor($this->data['Job']['id']/1000).'/'.$this->data['Job']['id'].'/'.$this->data['Job']['attention_img_file'];
				}else{
					$this->data['Job']['job_img_path']=null;
				}
				if(!empty($this->data['Job']['email1'])){
					$this->data['Job']['email1_flg']=1;
				}
				if(!empty($this->data['Job']['email2'])){
					$this->data['Job']['email2_flg']=1;
				}
				if(!empty($this->data['Job']['email3'])){
					$this->data['Job']['email3_flg']=1;
				}			
				$pickupareas=$this->JobPickupArea->findAll('"JobPickupArea"."job_id"='.$this->data['Job']['id'].' and "JobPickupArea"."deleted" is null');
			 	if(!empty($pickupareas)){
			 	$arr=array();
			 	$arr1=array();
			 	foreach($pickupareas as $area){	 		
			 		$arr[]=$area['JobPickupArea']['area_id'];
			 		$pa=$this->SysParams->find("\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"id\"=".$area['JobPickupArea']['area_id']);
			 		$arr1[]=trim($pa['SysParams']['name']);
			 	}
			 	$str=implode(',',$arr);
			 	$str1=implode(',',$arr1);	 	
			 	$this->data['Job']['address1'] = $str1;
			 	$this->data['Job']['job_pickup_addr']=$str;
			 	}
			 	
			 	$pickuptemas=$this->JobPickupThema->findAll('"JobPickupThema"."job_id"='.$this->data['Job']['id'].' and "JobPickupThema"."deleted" is null');
			 	if($pickuptemas){
			 	$arr=array();
			 	$arr1=array();
			 	foreach($pickuptemas as $tema){	 		
			 		$arr[]=$tema['JobPickupThema']['tema_id'];
			 		$pa=$this->SysParams->find("\"SysParams\".\"kbn\"='tema_id' and \"SysParams\".\"id\"=".$tema['JobPickupThema']['tema_id']);
			 		$arr1[]=trim($pa['SysParams']['name']);
			 	}
			 	$str=implode(',',$arr);
			 	$str1=implode(',',$arr1);	 	
			 	$this->data['Job']['tema1'] = $str1;
			 	$this->data['Job']['job_pickup_tema']=$str;
			 	}
			 	
			 	$attentionareas=$this->JobAttentionArea->findAll('"JobAttentionArea"."job_id"='.$this->data['Job']['id'].' and "JobAttentionArea"."deleted" is null');
			 	if(!empty($attentionareas)){
			 	$arr=array();
			 	$arr1=array();
			 	foreach($attentionareas as $area){	 		
			 		$arr[]=$area['JobAttentionArea']['area_id'];
			 		$pa=$this->SysParams->find("\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"id\"=".$area['JobAttentionArea']['area_id']);
			 		$arr1[]=trim($pa['SysParams']['name']);
			 	}
			 	$str=implode(',',$arr);
			 	$str1=implode(',',$arr1);	 	
			 	$this->data['Job']['address2'] = $str1;
			 	$this->data['Job']['job_attention_addr']=$str;
			 	}
			 	
			 	$attentiontemas=$this->JobAttentionThema->findAll('"JobAttentionThema"."job_id"='.$this->data['Job']['id'].' and "JobAttentionThema"."deleted" is null');
			 	if($attentiontemas){
			 	$arr=array();
			 	$arr1=array();
			 	foreach($attentiontemas as $tema){	 		
			 		$arr[]=$tema['JobAttentionThema']['tema_id'];
			 		$pa=$this->SysParams->find("\"SysParams\".\"kbn\"='tema_id' and \"SysParams\".\"id\"=".$tema['JobAttentionThema']['tema_id']);
			 		$arr1[]=trim($pa['SysParams']['name']);
			 	}
			 	$str=implode(',',$arr);
			 	$str1=implode(',',$arr1);	 	
			 	$this->data['Job']['tema2'] = $str1;
			 	$this->data['Job']['job_attention_tema']=$str;
			 	}
				
				$posts=$this->JobPost->findAll('"JobPost"."job_id"='.$this->data['Job']['id'].' and "JobPost"."deleted" is null');
			 	$arr=array();
			 	foreach($posts as $post){	 		
			 		$arr[]=$post['JobPost']['post_id'];
			 	}	 	
			 	$this->data['Job']['post_type'] = $arr;
			 	
				$langs=$this->JobHopeLanguage->findAll('"JobHopeLanguage"."job_id"='.$this->data['Job']['id'].' and "JobHopeLanguage"."deleted" is null');
			 	$arr=array();
			 	foreach($langs as $lang){	 		
			 		$arr[]=$lang['JobHopeLanguage']['hope_language_id'];
			 	}	 	
			 	$this->data['Job']['hope_language'] = $arr;
			 	
			 	$mem_types=$this->JobHopeMember->findAll('"JobHopeMember"."job_id"='.$this->data['Job']['id'].' and "JobHopeMember"."deleted" is null');
			 	$arr=array();
			 	if(!empty($mem_types)){
				 	foreach($mem_types as $mem){	 		
				 		$arr[]=$mem['JobHopeMember']['hope_member_id'];
				 	}
			 	}	 	
			 	$this->data['Job']['hope_member'] = $arr;
			 	
			 	if($this->data['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){
				 	$jobareas=$this->JobArea->findAll('"JobArea"."job_id"='.$this->data['Job']['id'].' and "JobArea"."deleted" is null');
				 	$arr=array();
				 	foreach($jobareas as $area){	 		
				 		$arr[]=$area['JobArea']['job_area_id'];
				 	}			 		 	
				 	$this->data['Job']['job_area'] = $arr;
				 	
				 	$categories=$this->JobCategory->findAll('"JobCategory"."job_id"='.$this->data['Job']['id'].' and "JobCategory"."deleted" is null');
				 	$arr=array();
				 	foreach($categories as $category){	 		
				 		$arr[]=$category['JobCategory']['category_id'];
				 	}	 	
				 	$this->data['Job']['category'] = $arr;
			 	}
			 	$attentions=$this->JobAttention->findAll('"JobAttention"."job_id"='.$this->data['Job']['id'].' and "JobAttention"."deleted" is null');
			 	$arr=array();
			 	foreach($attentions as $attention){	 		
			 		$arr[]=$attention['JobAttention']['attention_id'];
			 	}	 	
			 	$this->data['Job']['attention'] = $arr;			 	
			}else{
		 		$this->data=$this->Session->read('job');
		 		if(!empty($this->data['Job']['job_pickup_addr'])){
			    $this->data['Job']['job_pickup_addr']=implode(',',$this->data['Job']['job_pickup_addr']);
			    }
			    if(!empty($this->data['Job']['job_pickup_tema'])){
			    $this->data['Job']['job_pickup_tema']=implode(',',$this->data['Job']['job_pickup_tema']);
			    }
			    if(!empty($this->data['Job']['job_attention_addr'])){
			    $this->data['Job']['job_attention_addr']=implode(',',$this->data['Job']['job_attention_addr']);
			    }
			    if(!empty($this->data['Job']['job_attention_tema'])){
			    $this->data['Job']['job_attention_tema']=implode(',',$this->data['Job']['job_attention_tema']);
			    }
		 	}
	 	}else{	 		
	 	    $form=$this->params['form'];
		    $job_info=$form['data'];
		    if($this->data['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){
			    if(!empty($job_info['Job']['job_pickup_addr'])){
			    $job_info['Job']['job_pickup_addr']=explode(',',$job_info['Job']['job_pickup_addr']);
			    }
			    if(!empty($job_info['Job']['job_pickup_tema'])){
			    $job_info['Job']['job_pickup_tema']=explode(',',$job_info['Job']['job_pickup_tema']);
			    }
			    if(!empty($job_info['Job']['job_attention_addr'])){
			    $job_info['Job']['job_attention_addr']=explode(',',$job_info['Job']['job_attention_addr']);
			    }
			    if(!empty($job_info['Job']['job_attention_tema'])){
			    $job_info['Job']['job_attention_tema']=explode(',',$job_info['Job']['job_attention_tema']);
			    }						
		 		$message=$this->Job->valid($this->Valid,$job_info);
		 			 		
		 		if($job_info['Job']['job_img_del']==0){			
				    $max_file_size=$this->Prop->getValue('config.upload_size');
					$img_message = $this->Valid->ErrorMsg($this->params['form'], 'job_img_file/error', 'upload_error');
					$img_message .= $this->Valid->ErrorMsg($this->params['form'], 'job_img_file/type', 'image_type');
					$img_message .= $this->Valid->ErrorMsg($this->params['form'], 'job_img_file/size', 'file_size', $max_file_size);
					$message.=$img_message;
			    }   	 			
		    }else{
		    	$message=$this->Job->Job_Add_Valid($this->Valid,$this->data);
		    }
		    if($message){
			$this->set('message', $message);
			$this->edit_init($job_info['Job']['id']);
			$this->render('job_edit');
			exit;
		    }
		    if(isset($job_info['Job']['company_name_flg'])&&$job_info['Job']['company_name_flg']!=2){
	 			$job_info['Job']['company_name_other']=null;
	 		}
		    if($job_info['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){	
			    if(!empty($form['job_img_file']['name']['0']) && $job_info['Job']['job_img_del']==0){
				    $this->Image->dir_confirm=array($this->Prop->getValue('config.job.image.dir_confirm'));
					$this->Image->rdImage->resizeWidth = array('128');
					$this->Image->rdImage->resizeHeight = array('100');
					if ($this->Image->saveConfirmPhoto($form, 'job_img_file')) {
						$dir_confirm = $this->Image->dir_confirm;
						$filename = $this->Image->filename;
						$job_info['Job']['job_img'] = $filename[0];
				    }
				}
		        if($job_info['Job']['job_img_del']==0){ 
		        	if(!empty($form['job_img_file']['name']['0'])){   
	                $job_info['Job']['job_img_path']='/'.$this->Prop->getValue('config.job.image.dir_confirm').'/'.$job_info['Job']['job_img'];
	                }
				}else{
					$job_info['Job']['job_img_path']=null;
				}
		    }
		    $this->Session->write('job',$job_info);
		    
		    $this->redirect('/job_info/job_edit_confirm');
	 	}	 		
	}
	/**
	 * @author lizeqiu
	 * @desc job编辑确认
	 * */
	function job_edit_confirm(){
		$job_info=$this->Session->read('job');
		if(empty($job_info)){
			$this->render('../errors/error404');
			exit;
		}
		$this->confirm_init($job_info);			 
	}
	/**
	 * @author lizeqiu
	 * @desc  job编辑信息保存 
	 * */
	function job_edit_save(){
	    $job_info=$this->Session->read('job');
	    if(empty($job_info)){
			$this->render('../errors/error404');
			exit;
		}
		
		
//20081226
//		$account=$this->CompanyAccount->find('CompanyAccount.id='.$job_info['Job']['admin_id']);
//		if($job_info['Job']['email1_flg']==1){
//			$job_info['Job']['email1']=$account['CompanyAccount']['email'];
//		}else{
//			$job_info['Job']['email1']=null;
//		}

		//取得公司信息	    		
	    $company=$this->Company->find('"Company"."id"='.$job_info['Job']['company_id']);
	    
	    //確認用メールアドレース
	    $job_info['Job']['email1']=$company['Company']['email'];
	    
	    //表示用メールアドレース
		if($job_info['Job']['email2_flg']==1){
			$job_info['Job']['email2']=$company['Company']['incept_email'];
		}else{
			$job_info['Job']['email2']=null;
		}
		
		//応募先（その他）
		if($job_info['Job']['email3_flg']==0){
			$this->Job->delEmail3($job_info['Job']['id']);
			$job_info['Job']['email3']=null;
		}
		
		//雇用形態
		$job_info['Job']['post_type_id']=$job_info['Job']['post_type']['0'];
		
		//注目求人写真
		if($job_info['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){
			if(!empty($job_info['Job']['job_img'])){
			$job_info['Job']['attention_img_file']=$job_info['Job']['job_img'];
			}
		}
		
		//応募資格・語学力
		if(!empty($job_info['Job']['hope_language'])){
			$lang_flg=0;
			foreach($job_info['Job']['hope_language'] as $index=>$hope){
			if($job_info['Job']['hope_language'][$index]==4){
				$lang_flg=1;
			}
			}
			if($lang_flg==0){
				$this->Job->delLangNote($job_info['Job']['id']);
				$job_info['Job']['hope_language_note']=null;
			}
		}
		
		//掲載期間
		if(empty($job_info['Job']['public_start'])){
			$job_info['Job']['public_start']=null;
		}
		if(empty($job_info['Job']['public_end'])){
			$job_info['Job']['public_end']=null;
		}		
		
		
		if(empty($job_info['Job']['important_tema_id'])){
			$job_info['Job']['important_tema_id']=null;
		}
		
		//PICKUP期間
		if($job_info['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){
			if(empty($job_info['Job']['pickup_start'])){
				$job_info['Job']['pickup_start']=null;
			}
			if(empty($job_info['Job']['pickup_end'])){
				$job_info['Job']['pickup_end']=null;
			}
			if(empty($job_info['Job']['attention_start'])){
				$job_info['Job']['attention_start']=null;
			}
			if(empty($job_info['Job']['attention_end'])){
				$job_info['Job']['attention_end']=null;
			}
		}
		$job_info['Job']['job_status_id1']=$job_info['Job']['job_status_id'];
		/*if($this->rdAuth->role!='System'&&$job_info['Job']['job_status_id']==1&&$job_info['Job']['public_start']<date('Y-m-d',time())&&$job_info['Job']['public_end']>date('Y-m-d',time())){
			$job_info['Job']['job_status_id']=0;		
		}elseif($job_info['Job']['job_status_id']==2){
			if($this->rdAuth->role!='System'){
				$job_info['Job']['job_status_id']=0;
			}elseif($job_info['Job']['public_start']<date('Y-m-d',time())&&$job_info['Job']['public_end']>date('Y-m-d',time())){
				$job_info['Job']['job_status_id']=1;
			}
		}*/
		if($job_info['Job']['job_status_id']==2){
			$job_info['Job']['job_status_id']=0;
		}

		if($this->Job->save($job_info)){
			$job_id=$job_info['Job']['id'];	
			if($job_info['Job']['job_status_id1']==1&&$this->rdAuth->role=='System'){		    
			    if($job_info['Job']['job_img_del']==1){
			    	$this->Job->delImgFile($job_id);
			    }
			    if($job_info['Job']['job_img_del']==0 && !empty($job_info['Job']['job_img'])){
				$this->Image->dir_confirm=array($this->Prop->getValue('config.job.image.dir_confirm'));
				$path = floor($job_id/1000). DS .$job_id;
				$this->Image->dir=array($this->Prop->getValue('config.job.image.dir'));
				$this->Image->savePhoto($path,$job_info['Job']['job_img']);		
				}
			    if(!empty($job_info['Job']['job_pickup_addr'])){    	
				    $this->Job->delAttentionInfo($job_id);
				    $this->JobAttentionArea->delByJobid($job_id);
				    $this->JobPickupArea->delByJobid($job_id);
					foreach($job_info['Job']['job_pickup_addr'] as $pickup_area){
					$this->JobPickupArea->save(array("job_id"=>$job_id,"area_id"=>$pickup_area));
					}
				}else{
					$this->JobPickupArea->delByJobid($job_id);
				}
			    if(!empty($job_info['Job']['job_pickup_tema'])){		    	
				    $this->JobAttentionThema->delByJobid($job_id);
				    $this->JobPickupThema->delByJobid($job_id);
					foreach($job_info['Job']['job_pickup_tema'] as $pickup_tema){
					$this->JobPickupThema->save(array("job_id"=>$job_id,"tema_id"=>$pickup_tema));
					}
				}else{
					$this->JobPickupThema->delByJobid($job_id);
				}
				if(!empty($job_info['Job']['job_attention_addr'])){    	
				    $this->Job->delPickupInfo($job_id);
				    $this->JobPickupArea->delByJobid($job_id);
				    $this->JobAttentionArea->delByJobid($job_id);
					foreach($job_info['Job']['job_attention_addr'] as $attention_area){
					$this->JobAttentionArea->save(array("job_id"=>$job_id,"area_id"=>$attention_area));
					}
				}else{
					$this->JobAttentionArea->delByJobid($job_id);
				}
				if(!empty($job_info['Job']['job_attention_tema'])){    	
				    $this->JobPickupThema->delByJobid($job_id);
				    $this->JobAttentionThema->delByJobid($job_id);
					foreach($job_info['Job']['job_attention_tema'] as $attention_tema){
					$this->JobAttentionThema->save(array("job_id"=>$job_id,"tema_id"=>$attention_tema));
					}
				}else{
					$this->JobAttentionThema->delByJobid($job_id);
				}
			}
		    $this->JobPost->delByJobid($job_id);
			foreach($job_info['Job']['post_type'] as $post_type){
			$this->JobPost->save(array("job_id"=>$job_id,"post_id"=>$post_type));
			}
			$this->JobHopeMember->delByJobid($job_id);
			if(!empty($job_info['Job']['hope_member'])){
				foreach($job_info['Job']['hope_member'] as $hope_member){
				$this->JobHopeMember->save(array("job_id"=>$job_id,"hope_member_id"=>$hope_member));
				}
			}		
			if($job_info['Job']['job_status_id1']==1&&$this->rdAuth->role=='System'){
				$this->JobArea->delByJobid($job_id);
				foreach($job_info['Job']['job_area'] as $job_area){
				$this->JobArea->save(array("job_id"=>$job_id,"job_area_id"=>$job_area));
				}
			}
			
			$this->JobHopeLanguage->delByJobid($job_id);
			if(!empty($job_info['Job']['hope_language'])){
				foreach($job_info['Job']['hope_language'] as $hope_language){
				$this->JobHopeLanguage->save(array("job_id"=>$job_id,"hope_language_id"=>$hope_language));
				}
			}
			if($job_info['Job']['job_status_id1']==1&&$this->rdAuth->role=='System'){
				$this->JobCategory->delByJobid($job_id);
				foreach($job_info['Job']['category'] as $category){
				$this->JobCategory->save(array("job_id"=>$job_id,"category_id"=>$category));
				}
			}
			$this->JobAttention->delByJobid($job_id);
			if(!empty($job_info['Job']['attention'])){
				foreach($job_info['Job']['attention'] as $attention){
				$this->JobAttention->save(array("job_id"=>$job_id,"attention_id"=>$attention));
				}		
			}
		}
		$this->Session->del('job');
		$this->redirect('job_info/job_edit_finish');
		//$this->render('job_edit_finish');	
	}
	function job_edit_finish(){
		$status=$this->Session->read('job_status');
		$page=$this->Session->read('job_status_page');
		$this->set('status',$status);
		$this->set('page',$page);
		$this->Session->del('job_status');
		$this->Session->del('job_status_page');
	}
	function job_add_search(){}
	
	function company_search_list(){
		if(!empty($this->data)){
			$condition="\"Company\".\"id\"=###company_id### AND" .
					   " \"Company\".\"company_name\" like '%###company_name###%' AND" .
					   " \"Company\".\"company_alias\" like '%###company_alias###%' AND" .
					   " \"Company\".\"deleted\" is null and \"Company\".\"company_type_id\" is not null";
			$condition=$this->Search->calulate($condition,$this->data['JobInfo']);
			$this->Session->write('search_condition',$condition);
		}else{
			$condition=$this->Session->read('search_condition');
		}
		$this->Company->belongsTo=array();
		$companies=$this->Company->findAll($condition,null,'Company.created desc',$this->show,$this->page);
		$count=$this->Company->findCount($condition);
		$this->set('companies',$companies);
		$this->set('page',$this->page);
		$this->setPaging('ajax','/job_info/company_search_list',$count);
	}
	
	/**
	 * @author lizeqiu
	 * @desc job添加
	 * */
	function job_add($company_id=null,$page=1,$flg=null){
		$post_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'post_id' and \"SysParams\".\"deleted\" is null");
		$this->set('post_data',$post_data);
		$lang_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'hope_language_id' and \"SysParams\".\"deleted\" is null",'sort');
		$this->set('lang_data',$lang_data);
		$memb_type = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'hope_member_id' and \"SysParams\".\"deleted\" is null");
		$this->set('member_type',$memb_type);
		$attention_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'attention_id' and \"SysParams.\"deleted\" is null");
		$this->set('attention_data',$attention_data);
		if(empty($this->data)){
			if(empty($company_id)&&$this->rdAuth->role!='System'){
				$company=$this->Company->find('"Company"."id"='.$this->rdAuth->company_id);
			}else{
				$this->checkAccess1($company_id,$this->rdAuth->company_id);
				$company=$this->Company->find('"Company"."id"='.$company_id);
			}
			$this->set('page',$page);
			$this->set('company',$company);	
			//pr($company);	
		}		
		
		if(empty($this->data)){
			$this->render('job_add');
			exit;
		}
		$message=$this->Job->Job_Add_Valid($this->Valid,$this->data);
		if($this->data['Job']['email3_flg']==1&&empty($this->data['Job']['email3'])){
			$message.=$this->Valid->TagErrorMsg('required','Job/email3');
		}
		if($this->data['Job']['company_name_flg']==2){
			$message.=$this->Valid->ErrorMsg($this->data,'Job/company_name_other','required');
			$message.=$this->Valid->ErrorMsg($this->data,'Job/company_name_other','maxlength','100');
		}else{
			$this->data['Job']['company_name_other']=null;
		}
		if(!empty($this->data['Job']['hope_language'])){
			foreach($this->data['Job']['hope_language'] as $index=>$hope){
				if($this->data['Job']['hope_language'][$index]==4&&empty($this->data['Job']['hope_language_note'])){
					$message.=$this->Valid->TagErrorMsg('required','Job/hope_language_note');
				}
		    }
		}
		$company=$this->Company->find('Company.id='.$this->data['Job']['company_id']);
		if(!empty($flg)&&$flg==2&&$this->rdAuth->role!='System'){		
	    	if((!empty($company['Company']['job_approved'])||$company['Company']['job_approved']==0)&&$company['Company']['job_approved']<=0){
				$message.= $this->Valid->tagErrorMsg('approve_count',$company['Company']['week_job_count']);
			}
	    }
	    if(!empty($flg)&&$flg==1){
		    $this->data['Job']['job_status_id']=3;
	    }
	    if($message){	    	
			$this->set('message', $message);
			$this->set('page',$page);
	    	$this->set('company',$company);
			$this->render('job_add');
			exit;
	    }
	    $this->Session->write('job_info',$this->data);
	    $this->redirect('job_info/job_add_confirm');	
	}
	/**
	 * @author lizeqiu
	 * @desc job添加与编辑的确认页面初始化
	 * */	
	function confirm_init($job_info){
		$company=$this->Company->find('"Company"."id"='.$job_info['Job']['company_id']);
//edit by yangjie 090324
//		if($job_info['Job']['email1_flg']==1){
//			if($this->rdAuth->role!='System'){
//				if(isset($job_info['Job']['admin_id'])&&!empty($job_info['Job']['admin_id'])){
//					$account=$this->CompanyAccount->find('CompanyAccount.id='.$job_info['Job']['admin_id']);
//				}
//				else{
//					$account=$this->CompanyAccount->find('CompanyAccount.id='.$this->rdAuth->id);
//				}
//			}else{
//				$account=$this->CompanyAccount->find("CompanyAccount.role='Admin'"." and CompanyAccount.company_id=".$job_info['Job']['company_id']);
//			}
//			$job_info['Job']['email1']=$account['CompanyAccount']['email'];
//		}else{
//			$job_info['Job']['email1']=null;
//		}

		$job_info['Job']['email1']=$company['Company']['email'];
		if($job_info['Job']['email2_flg']==1){
			$job_info['Job']['email2']=$company['Company']['incept_email'];
		}else{
			$job_info['Job']['email2']=null;
		}
		if($job_info['Job']['email3_flg']==0){
			$job_info['Job']['email3']=null;
		}
		$this->set('company',$company);
		$this->set('job',$job_info);
		//		
		$sql="SysParams.kbn='post_id' and SysParams.deleted is null and SysParams.id in (";
        foreach($job_info['Job']['post_type'] as $jarea){
        	$sql=$sql.$jarea.",";
        }
        $sql=substr($sql,0,strlen($sql)-1).")"; 
        $arr=array();
        $paras=$this->SysParams->findAll($sql);
        for($i=0;$i<count($paras);$i++){
        $arr[$i]=$paras[$i]['SysParams']['name'];
        }
        $this->set('post_type',implode('，',$arr));
		//
		if(!empty($job_info['Job']['hope_member'])){
	        $sql="SysParams.kbn='hope_member_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['hope_member'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        $arr[$i]=$paras[$i]['SysParams']['name'];
	        }
	        $this->set('hope_members',implode('，',$arr));
        }
        //
       if(!empty($job_info['Job']['attention'])){
	        $sql="SysParams.kbn='attention_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['attention'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        $arr[$i]=$paras[$i]['SysParams']['name'];
	        }
	        $this->set('attentions',implode('，',$arr));
       }
	    //
	    if($job_info['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){
		    $sql="SysParams.kbn='area_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['job_area'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);	        
	        for($i=0;$i<count($paras);$i++){
	        $arr[$i]=$paras[$i]['SysParams']['name'];
	        }
	        if(!empty($job_info['Job']['important_area_id'])){
	        $sys=$this->SysParams->find("\"SysParams\".\"kbn\"='area_id' and \"SysParams\".\"deleted\" is null and \"SysParams\".\"id\"=".$job_info['Job']['important_area_id']);
	        $this->set('important_area',$sys['SysParams']['name']);
	        }
	        $this->set('areas',implode('，',$arr)); 
	        
	    }
        //
        if(!empty($job_info['Job']['hope_language'])){
	        $sql="SysParams.kbn='hope_language_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['hope_language'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        	if($paras[$i]['SysParams']['id']!=4){
	                $arr[$i]=$paras[$i]['SysParams']['name'];
	             }else{
	             	$arr[$i]=$job_info['Job']['hope_language_note'];
	             }
	        }
	        $this->set('languages',implode('，',$arr));       
        }
        //
        if($job_info['Job']['job_status_id']==1&&$this->rdAuth->role=='System'){
	        $sql="SysParams.kbn='tema_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['category'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        $arr[$i]=$paras[$i]['SysParams']['name'];
	        }
	        $this->set('categories',implode('，',$arr));
        }
	}
	/**
	 * @author lizeqiu
	 * @desc job添加确认
	 * */
	function job_add_confirm(){
		$job_info=$this->Session->read('job_info');
		$company=$this->Company->find('"Company"."id"='.$job_info['Job']['company_id']);
//by20081217
//		if($job_info['Job']['email1_flg']==1){
//			if($this->rdAuth->role!='System'){
//			$account=$this->CompanyAccount->find('CompanyAccount.id='.$this->rdAuth->id);
//			}else{
//				$account=$this->CompanyAccount->find("CompanyAccount.role='Admin'"." and CompanyAccount.company_id=".$job_info['Job']['company_id']);
//			}
//			$job_info['Job']['email1']=$account['CompanyAccount']['email'];
//		}else{
//			$job_info['Job']['email1']=null;
//		}
		$job_info['Job']['email1']=$company['Company']['email'];
		if($job_info['Job']['email2_flg']==1){
			$job_info['Job']['email2']=$company['Company']['incept_email'];
		}else{
			$job_info['Job']['email2']=null;
		}
		if($job_info['Job']['email3_flg']==0){
			$job_info['Job']['email3']=null;
		}
		$this->set('company',$company);
		$this->set('job',$job_info);
		//		
		$sql="SysParams.kbn='post_id' and SysParams.deleted is null and SysParams.id in (";
        foreach($job_info['Job']['post_type'] as $jarea){
        	$sql=$sql.$jarea.",";
        }
        $sql=substr($sql,0,strlen($sql)-1).")"; 
        $arr=array();
        $paras=$this->SysParams->findAll($sql);
        for($i=0;$i<count($paras);$i++){
        $arr[$i]=$paras[$i]['SysParams']['name'];
        }
        $this->set('post_type',implode('，',$arr));
		//
		if(!empty($job_info['Job']['hope_member'])){
	        $sql="SysParams.kbn='hope_member_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['hope_member'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        $arr[$i]=$paras[$i]['SysParams']['name'];
	        }
	        $this->set('hope_members',implode('，',$arr));
        }
        //
        if(!empty($job_info['Job']['attention'])){
	        $sql="SysParams.kbn='attention_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['attention'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        $arr[$i]=$paras[$i]['SysParams']['name'];
	        }
	        $this->set('attentions',implode('，',$arr));
        }
        //
        if(!empty($job_info['Job']['hope_language'])){
	        $sql="SysParams.kbn='hope_language_id' and SysParams.deleted is null and SysParams.id in (";
	        foreach($job_info['Job']['hope_language'] as $jarea){
	        	$sql=$sql.$jarea.",";
	        }
	        $sql=substr($sql,0,strlen($sql)-1).")"; 
	        $arr=array();
	        $paras=$this->SysParams->findAll($sql);
	        for($i=0;$i<count($paras);$i++){
	        	if($paras[$i]['SysParams']['id']!=4){
	                $arr[$i]=$paras[$i]['SysParams']['name'];
	             }else{
	             	$arr[$i]=$job_info['Job']['hope_language_note'];
	             }
	        }
	        $this->set('languages',implode('，',$arr));  
        }
        if(!empty($this->data)){                        
			if(!isset($job_info['Job']['job_status_id'])){	
				if((!empty($company['Company']['job_approved'])||$company['Company']['job_approved']==0)&&$company['Company']['job_approved']<=0&&$this->rdAuth->role!='System'){
					$message= $this->Valid->tagErrorMsg('approve_count',$company['Company']['week_job_count']);
					$this->set('message',$message);
					$this->render('job_add_confirm');
					exit;
				}else{
					$this->redirect('job_info/job_add_save');
				}
			}else{ 
	        	$this->redirect('job_info/job_add_save');
        	}
        }
        
	} 
	/**
	 * @author lizeqiu
	 * @desc job添加信息保存
	 * */
	function job_add_save($flg=null){
		$job_info=$this->Session->read('job_info');
//		if(!empty($flg)){
//			$job_info['Job']['job_status_id']=3;
//		}		
		if($this->rdAuth->role!='System'){
			$account=$this->CompanyAccount->find('"CompanyAccount"."id"='.$this->rdAuth->id);
		}else{
			$comp=$this->Company->find('Company.id='.$job_info['Job']['company_id']);
			if($comp['Company']['company_type_id']==1){
				$account=$this->CompanyAccount->find("\"CompanyAccount\".\"role\"='Admin'"." and \"CompanyAccount\".\"company_id\"=".$job_info['Job']['company_id'],null,'CompanyAccount.id asc');
			}else{
				$account=$this->CompanyAccount->find("\"CompanyAccount\".\"role\"='General'"." and \"CompanyAccount\".\"company_id\"=".$job_info['Job']['company_id'],null,'CompanyAccount.id asc');
			} 
		}
		
		$job_info['Job']['admin_id']=$account['CompanyAccount']['id'];
//by20081217
		$company=$this->Company->find('"Company"."id"='.$job_info['Job']['company_id']);
		
		//確認用メールアドレース
		$job_info['Job']['email1']=$company['Company']['email'];
		
		//表示用メールアドレース
		if($job_info['Job']['email2_flg']==1){
			$job_info['Job']['email2']=$company['Company']['incept_email'];
		}
		
		$this->Job->create();
		if($this->Job->save($job_info['Job'])){
			$job_id=$this->Job->getLastInsertID();
			foreach($job_info['Job']['post_type'] as $post_type){
			$this->JobPost->create();
			$this->JobPost->save(array("job_id"=>$job_id,"post_id"=>$post_type));
			}
			if(!empty($job_info['Job']['hope_member'])){
				foreach($job_info['Job']['hope_member'] as $hope_member){
				$this->JobHopeMember->create();
				$this->JobHopeMember->save(array("job_id"=>$job_id,"hope_member_id"=>$hope_member));
				}
			}
			if(!empty($job_info['Job']['hope_language'])){
				foreach($job_info['Job']['hope_language'] as $hope_language){
				$this->JobHopeLanguage->create();
				$this->JobHopeLanguage->save(array("job_id"=>$job_id,"hope_language_id"=>$hope_language));
				}
			}
			if(!empty($job_info['Job']['attention'])){
				foreach($job_info['Job']['attention'] as $attention){
				$this->JobAttention->create();
				$this->JobAttention->save(array("job_id"=>$job_id,"attention_id"=>$attention));
				}	
			}
				
			//一周投稿数减一
			$company = $this->Company->find('"Company"."id"='.$job_info['Job']['company_id']);
			if(!empty($company['Company']['job_approved'])){
				$approve=$company['Company']['job_approved']-1;
			    $this->Company->approveDesc($company['Company']['id'],$approve);
			}
			
			//求人受付メール
			if(!isset($job_info['Job']['job_status_id'])||$job_info['Job']['job_status_id']!=3){
				$subject=$this->Prop->getValue('config.mail_title.job_add_finish');
				$subject=str_replace('#company_name_alias#',$company['Company']['company_alias'], $subject);		
	            $job=$this->Job->find('"Job"."id"='.$job_id,null,null,2);
				$contents=$this->getMailTemplate("job_add_finish",$job);
				
				/**
				 * @2009-04-01
				 * 1.確認用メールアドレス	        届かない
				 * 2.求人投稿担当者メールアドレス　　	届く
				 * 3.表示用メールアドレス	        届かない
                 * 4.別のメールアドレス          　　届かない
				 */
				 
				//求人投稿担当者メールアドレス
				//カモメさんが管理者（system）としてログインした場合はメール送信は不要です。
				if($this->rdAuth->role!='System'){
					//$this->MailJob->saveMsg('web@localhost:82',$this->rdAuth->email,$subject,$contents,'shaozhuojun@natec.cn','info@localhost:82');
					$this->MailJob->saveMsg('web@localhost:82',$this->rdAuth->email,$subject,$contents,'x.nan.kuifeng@nssol-sh.com','info@localhost:82');
				}
				
				//確認用メールアドレス
//				if(!empty($job_info['Job']['email1'])){
//					$this->MailJob->saveMsg('web@localhost:82',$job_info['Job']['email1'],$subject,$contents,'recruit@localhost:82','info@localhost:82');
//				}
				//表示用メールアドレス
//				if($job_info['Job']['email2_flg']==1 && !empty($job_info['Job']['email2'])){
//					$this->MailJob->saveMsg('web@localhost:82',$job_info['Job']['email2'],$subject,$contents,'recruit@localhost:82','info@localhost:82');
//				}
				//別のメールアドレス 
//				if($job_info['Job']['email3_flg']==1){
//					$this->MailJob->saveMsg('web@localhost:82',$job_info['Job']['email3'],$subject,$contents,'recruit@localhost:82','info@localhost:82');
//				}
			}
			//$this->MailJob->saveMsg1('info@localhost:82',$subject,$contents);
		}
		$this->Session->del('search_condition');
		//$this->Session->del('job_info');
		$this->redirect('job_info/job_add_finish');
	}
	function job_add_finish(){}
	/**
	 * @author lizeqiu
	 * @desc 地域选择列表
	 * */
	function address_list($addr_flg){
			
	    $this->layout = 'ajax'; 
		$area_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'area_id' and \"SysParams\".\"deleted\" is null");

		$this->set('areas',$area_data);
		$this->set('flg',$addr_flg);		
	}
	/**
	 * @author lizeqiu
	 * @desc 工作种类列表
	 * */
	function tema_list($atten_flg){
		$this->layout = 'ajax'; 
		$tema_data = $this->SysParams->generateList("\"SysParams\".\"kbn\" = 'tema_id' and \"SysParams\".\"deleted\" is null");

		$this->set('temas',$tema_data);
		$this->set('flg',$atten_flg);	
	}
	
	function temp_job_list(){
		$condition="\"Job\".\"job_status_id\"=3 and \"Job\".\"deleted\" is null";
		$company_id=$this->rdAuth->company_id;
		
		if($this->rdAuth->role=='Admin'){
			$company_id=$this->rdAuth->company_id;
			$condition.=" and \"Job\".\"company_id\"=$company_id";
		}
		if($this->rdAuth->role=='Staff'){
			$account_id=$this->rdAuth->id;
			$condition.=" and \"Job\".\"admin_id\"=$account_id";
		}
		$temp_jobs=$this->Job->findAll($condition,null,'Job.created desc',$this->show,$this->page);
		$count=$this->Job->findCount($condition);
		$this->setJobCookie($temp_jobs);
		$this->set('tempJobList',$temp_jobs);
		$this->set('page',$this->page);
		$this->setPaging('ajax','/job_info/temp_job_list',$count);
		
	}
	
	function temp_job_delete($job_id=null){
		if(empty($job_id)){
			$delcs= $_COOKIE['temp_job_delete'];
		}else{
			$ajob=$this->Job->find('"Job"."id"='.$job_id);
			$this->checkAccess1($ajob['Job']['company_id'],$this->rdAuth->company_id);
			$delcs=$job_id;
		}
        $this->Job->deleteJob($delcs);
		$this->delCookie("temp_job_delete");		
		$this->redirect('job_info/temp_job_list');
	}
	
	function temp_job_detail($job_id,$page=1){
		$temp_job=$this->Job->find('"Job"."id"='.$job_id);
		$this->checkAccess1($temp_job['Job']['company_id'],$this->rdAuth->company_id);
		$temp_job['Job']['post_data']=$this->processInner2($temp_job,'JobPost','post_id');
		$temp_job['Job']['lang_data']=$this->processInner2($temp_job,'JobHopeLanguage','hope_language_id');
		$temp_job['Job']['member_data']=$this->processInner2($temp_job,'JobHopeMember','hope_member_id');
		$temp_job['Job']['attention_data']=$this->processInner2($temp_job,'JobAttention','attention_id');
		$this->set('job',$temp_job);
		$this->set('page',$page);
		$this->Session->write('detail_url','/'.$this->params['controller'].'/'.$this->params['action']);
	
	    if(!empty($this->data)){
	    	if($this->data['Company']['job_approved']<=0){
				$message= $this->Valid->tagErrorMsg('approve_count',$temp_job['Company']['week_job_count']);
				$this->set('message',$message);
				$this->render('temp_job_detail');
			}
			$this->redirect('/job_info/temp_job_contribute/'.$this->data['Job']['id']);
    	}
	}	
	function temp_job_contribute($job_id){
		$temp_job=$this->Job->find('"Job"."id"='.$job_id);
		$this->checkAccess1($temp_job['Job']['company_id'],$this->rdAuth->company_id);
		$this->Job->temp_job_contri($job_id);
		
		$account=$this->CompanyAccount->find("\"CompanyAccount\".\"role\"='Admin'"." and \"CompanyAccount\".\"company_id\"=".$temp_job['Job']['company_id'],null,'CompanyAccount.id asc');
		$subject=$this->Prop->getValue('config.mail_title.job_add_finish');
		$subject=str_replace('#company_name_alias#',$temp_job['Company']['company_alias'], $subject);		
        $job=$this->Job->find('"Job"."id"='.$job_id,null,null,2);
		$contents=$this->getMailTemplate("job_add_finish",$job);
		//$this->MailJob->saveMsg('web@localhost:82',$temp_job['Company']['email'],$subject,$contents,'shaozhuojun@natec.cn','info@localhost:82');
		$this->MailJob->saveMsg('web@localhost:82',$temp_job['Company']['email'],$subject,$contents,'x.nan.kuifeng@nssol-sh.com','info@localhost:82');
		
		$this->redirect('job_info/finish');
	}
	
	function finish(){}
	
	function setJobCookie(& $data) {
		$delete = array ();
		if (isset ($_COOKIE['temp_job_delete']))
			$delete = split(',', $_COOKIE['temp_job_delete']);
		foreach ($data as & $d) {
			$id = $d['Job']['id'];			
			if (in_array($id, $delete)) {
				$d['Job']['delete'] = $id;
			}
		}
	}
	/**
	 * 保存用户勾选的delete复选框
	 * */
	function setValue(& $data) {

		$delete = array ();

		if (isset ($_COOKIE['job_list_delete']))
			$delete = split(',', $_COOKIE['job_list_delete']);
		foreach ($data as & $d) {
			$id = $d['Job']['id'];
			if (in_array($id, $delete)) {
				$d['Job']['delete'] = $id;
			}
		}
	}
	
	function getHourSelect(){
		$hour = array();
		for($i=0;$i<24;$i++){
			if(strlen($i)<2){
				$hour['0'.$i]='0'.$i;
			}
			else{
				$hour[$i]=$i;
			}
		}
		return $hour;
	}
	
	function getMinSelect(){
		$min = array();
		for($i=0;$i<60;$i++){
			if(strlen($i)<2){
				$min['0'.$i]='0'.$i;
			}
			else{
				$min[$i]=$i;
			}
		}
		return $min;
	}
}
?>